package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingListService;
import com.itheima.utils.SMSUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/OrderSettingList")
public class OrderSettingListController {
    @Reference
    private OrderSettingListService orderSettingListService;
    @Reference
    private OrderService orderService;
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderSettingListService.findPage(queryPageBean);
        return pageResult;
    }

    //增加预约
    @RequestMapping("/add")
    public Result add(@RequestBody Map map) throws Exception {
        map.put("orderType",Order.ORDERTYPE_TELEPHONE);
        String telephone = (String) map.get("telephone");
        String setmealId= map.get("setmealId").toString();
        map.put("setmealId",setmealId);
        Result result=null;
        try {
            result = orderService.order(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新建预约失败");
        }
        if (result.isFlag()){//预约的结果成功
            try {
                //SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone, (String) map.get("orderDate"));
                //ClientException e
                System.out.println("你已成功预约,预约时间为:" + (String) map.get("orderDate"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }
    //确定预约
    @RequestMapping("/update")
    public Result update(Integer id){
        Map map=new HashMap();
        map.put("id",id);
        map.put("orderStatus",Order.ORDERSTATUS_YES);
        try {
            orderSettingListService.update(map);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
    //取消预约
    @RequestMapping("/delete")
    public Result delete(Integer [] ids,@RequestBody List<Map> list){
        List<Order> orderList=orderSettingListService.findMemberConditions(ids);//查询取消预约的用户手机
        for (Order s : orderList) {
            System.out.println(s.getMember().getPhoneNumber()+"和"+s.getName());
//            try {
//                SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,s.getMember().getPhoneNumber(),s.getName());
//            } catch (ClientException e) {
//                e.printStackTrace();
//            }
        }

        try {
            orderSettingListService.delete(ids,list);//取消预约
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"取消预约失败");
        }
        return new Result(true,"取消预约成功");
    }
}
